<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-17
 * Time: 14:06
 */

namespace app\src\message\push;


use app\src\message\interfaces\IMessage;
use app\src\user\logic\UcenterMemberLogic;

/**
 * 友盟推送消息
 * Class UMPushMessage
 * @author hebidu <email:346551990@qq.com>
 * @package app\src\message\push
 */
class UMPushMessage implements IMessage
{
    //ihome_android
    protected $Android_appkey = '582ff69bb27b0a5711000284';
    protected $Android_secret = '0gawtzm01ffafbxbqejzraywo4fjuupz';
    protected $Android_mode   = true;
    //ihome_ios
    protected $IOS_appkey = '581bfe12b27b0a304500224d';
    protected $IOS_secret = 'asidw0iykpcyiflvewyagvboffobsfij';
    protected $IOS_mode   = false;

    public function __construct($uid=0)
    {
        vendor("UMeng.UmengPushApi");
    }

    /**
     * @return mixed
     */
    function create()
    {
        
    }

    /**
     *
     * 推送消息给指定用户的所有设备
     * @param $uid
     * @param  $param array
     * @return array|mixed
     */
    function send()
    {

    }


    /**
     *
     * 推送消息给指定用户的所有设备
     * @param $uid
     * @param  $param array
     * @param array $after_open
     * @return array
     */
    public function send2($uid,$param,$after_open=['type'=>'go_app','param'=>'','extra'=>'']){
        //check uid
        $result = (new UcenterMemberLogic())->getInfo(['id' => $uid]);

        if($result['status']){
            if(empty($result['info'])) return $this->apiReturnErr('该uid用户不存在');
        }else{
            return $this->apiReturnErr($result['info']);
        }

        if(empty($param)){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"param"]));
        }
        if(empty($param['ticker'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['ticker']"]));
        }
        if(empty($param['title'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['title']"]));
        }
        if(empty($param['text'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['text']"]));
        }
        if(empty($param['alert'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['alert']"]));
        }

        //发送安卓消息
        $Android = new \UmengPushApi($this->Android_appkey,$this->Android_secret);
        $entity = [
            'alias_type'      =>'ihome',
            'alias'           =>$uid,
            'ticker'          =>$param['ticker'],
            'title'           =>$param['title'],
            'text'            =>$param['text'],
            'after_open'      =>$after_open['type'],
            'production_mode' =>$this->Android_mode,
        ];
        //自定义打开指定页面
        if($after_open['type'] == 'go_url') $entity['url'] = $after_open['param'];
        if($after_open['type'] == 'go_activity') $entity['activity'] = $after_open['param'];
        if(!empty($after_open['extra'])) $entity['payload']['extra'] = $after_open['extra'];

        $result_Android = $Android->sendAndroidCustomizedcast($entity);

        //发送IOS消息
        $IOS = new \UmengPushApi($this->IOS_appkey,$this->IOS_secret);
        $entity = [
            'alias_type'      =>'ihome',
            'alias'           =>$uid,
            'alert'           =>$param['alert'],
            'badge'           =>0,
            'sound'           =>'chime',
            'production_mode' =>$this->IOS_mode,
        ];
        if(!empty($after_open['extra'])) $entity['payload']['extra'] = $after_open['extra'];
        //自定义打开指定页面
        if($after_open['type'] == 'go_activity') $entity['payload']['after_open'] = $after_open['param'];

        $result_IOS = $IOS->sendIOSCustomizedcast($entity);


        if($result_Android['status'] || $result_IOS['status']){
            return $this->apiReturnSuc('发送成功');
        }else{
            return $this->apiReturnErr('Android:'.$result_Android['info'].'   IOS:'.$result_IOS['info']);
        }

    }

    /**
     * 推送消息给所有用户
     * @param $param array
     * @param array $after_open
     * @return array
     */
    public function sendAll($param,$after_open=array('type'=>'go_app','param'=>'','extra'=>'')){

        if(empty($param)){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"param"]));
        }
        if(empty($param['ticker'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['ticker']"]));
        }
        if(empty($param['title'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['title']"]));
        }
        if(empty($param['text'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['text']"]));
        }
        if(empty($param['alert'])){
            return $this->apiReturnErr(lang("lack_parameter",["param"=>"['alert']"]));
        }

        //发送安卓设备广播消息
        $Android = new \UmengPushApi($this->Android_appkey,$this->Android_secret);

        $entity = [
            'ticker'          =>$param['ticker'],
            'title'           =>$param['title'],
            'text'            =>$param['text'],
            'after_open'      =>$after_open['type'],
            'production_mode' =>$this->Android_mode,//empty($param['production_mode'])?false:$param['production_mode']
        ];

        //自定义打开指定页面
        if($after_open['type'] == 'go_url') $entity['url'] = $after_open['param'];
        if($after_open['type'] == 'go_activity') $entity['activity'] = $after_open['param'];
        if(!empty($after_open['extra'])) $entity['payload']['extra']['after_open'] = $after_open['extra'];

        $result_Android = $Android->sendAndroidBroadcast($entity);


        //发送IOS设备广播消息
        $IOS = new \UmengPushApi($this->IOS_appkey,$this->IOS_secret);

        $entity = [
            'alert'           =>$param['alert'],
            'badge'           =>0,
            'sound'           =>'chime',
            'production_mode' =>$this->IOS_mode,//empty($param['production_mode'])?false:$param['production_mode']
        ];

        //自定义打开指定页面
        if($after_open['type'] == 'go_activity') $entity['payload']['after_open'] = $after_open['param'];
        if(!empty($after_open['extra'])) $entity['payload']['extra']['after_open'] = $after_open['extra'];

        $result_IOS = $IOS->sendIOSBroadcast($entity);

        if($result_Android['status']&&$result_IOS['status']){
            return $this->apiReturnSuc('发送成功');
        }else{
            if(!$result_Android['status']){
                return $this->apiReturnErr($result_Android['info']);
            }else{
                return $this->apiReturnErr($result_IOS['info']);
            }
        }

    }


    /**
     * 返回错误结构
     * @param $info
     * @return array
     */
    function apiReturnErr($info) {
        return array('status' => false, 'info' => $info);
    }

    /**
     * 返回成功结构
     * @param $info
     * @return array
     */
    function apiReturnSuc($info) {
        return array('status' => true, 'info' => $info);
    }


}